﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RestaurateurConsole;

namespace RestaurateurConsole.Core
{
    public class ActivityHandler
    {
        DataEncapsulator Data;
        private ActivityHandler() { }
        public ActivityHandler(DataEncapsulator Data)
        {
            this.Data = Data;
        }

        public IEnumerable<Activity> Activities
        {
            get
            {
                return Data.DB.Activities.AsEnumerable();
            }
        }

        public IEnumerable<Activity> Filter(DateTime fromDate)
        {
            return from a in Data.DB.Activities
                   where a.Date >= fromDate
                   orderby a.Date descending
                   select a;
        }

        public IEnumerable<Activity> Filter(int accountID)
        {
            return from a in Data.DB.Activities
                   where a.CustID == accountID
                   orderby a.Date descending
                   select a;
        }

        public IEnumerable<Activity> Filter(int accountID, DateTime fromDate)
        {
            return from a in Data.DB.Activities
                   where a.CustID == accountID
                   where a.Date >= fromDate
                   orderby a.Date descending
                   select a;
        }

        public void Remove(int actID)
        {
            Activity a = Data.DB.Activities.First(A => A.ActivityID == actID);
            Data.DB.Activities.DeleteOnSubmit(a);
            Data.DB.SubmitChanges();
        }

        public void Add(int custID, int salesID)
        {
            int ActivityID;
            if (Data.DB.Activities.Count() == 0)
                ActivityID = 1;
            else
                ActivityID = Data.DB.Activities.Max(A => A.ActivityID) + 1;

            Activity newActivity = new Activity();
            newActivity.ActivityID = ActivityID;
            newActivity.CustID = custID;
            newActivity.Date = DateTime.Now;
            newActivity.SalesID = salesID;
            newActivity.Payment = null;

            Data.DB.Activities.InsertOnSubmit(newActivity);
            Data.DB.SubmitChanges();
        }

        public void Add(int custID, decimal money, bool IsPayment)
        {
            int ActivityID;
            if (Data.DB.Activities.Count() == 0)
                ActivityID = 1;
            else
                ActivityID = Data.DB.Activities.Max(A => A.ActivityID) + 1;

            Activity newActivity = new Activity();
            newActivity.ActivityID = ActivityID;
            newActivity.CustID = custID;
            newActivity.Date = DateTime.Now;
            newActivity.SalesID = null;
            if (IsPayment)
            {
                newActivity.Payment = money;
                newActivity.Purchase = null;
            }
            else
            {
                newActivity.Purchase = money;
                newActivity.Payment = null;
            }

            Data.DB.Activities.InsertOnSubmit(newActivity);
            Data.DB.SubmitChanges();
        }

        public void Add(int custID, int salesID, decimal money, bool IsPayment)
        {
            int ActivityID;
            if (Data.DB.Activities.Count() == 0)
                ActivityID = 1;
            else
                ActivityID = Data.DB.Activities.Max(A => A.ActivityID) + 1;

            Activity newActivity = new Activity();
            newActivity.ActivityID = ActivityID;
            newActivity.CustID = custID;
            newActivity.Date = DateTime.Now;
            newActivity.SalesID = salesID;
            if (IsPayment)
            {
                newActivity.Payment = money;
                newActivity.Purchase = null;
            }
            else
            {
                newActivity.Purchase = money;
                newActivity.Payment = null;
            }

            Data.DB.Activities.InsertOnSubmit(newActivity);
            Data.DB.SubmitChanges();
        }

        public void Add(int custID, int salesID, DateTime d)
        {
            int ActivityID;
            if (Data.DB.Activities.Count() == 0)
                ActivityID = 1;
            else
                ActivityID = Data.DB.Activities.Max(A => A.ActivityID) + 1;

            Activity newActivity = new Activity();
            newActivity.ActivityID = ActivityID;
            newActivity.CustID = custID;
            newActivity.Date = d;
            newActivity.SalesID = salesID;
            newActivity.Payment = null;

            Data.DB.Activities.InsertOnSubmit(newActivity);
            Data.DB.SubmitChanges();
        }

        public void Add(int custID, decimal money, bool IsPayment, DateTime d)
        {
            int ActivityID;
            if (Data.DB.Activities.Count() == 0)
                ActivityID = 1;
            else
                ActivityID = Data.DB.Activities.Max(A => A.ActivityID) + 1;

            Activity newActivity = new Activity();
            newActivity.ActivityID = ActivityID;
            newActivity.CustID = custID;
            newActivity.Date = d;
            newActivity.SalesID = null;
            if (IsPayment)
            {
                newActivity.Payment = money;
                newActivity.Purchase = null;
            }
            else
            {
                newActivity.Purchase = money;
                newActivity.Payment = null;
            }

            Data.DB.Activities.InsertOnSubmit(newActivity);
            Data.DB.SubmitChanges();
        }

        public void Add(int custID, int? salesID, decimal money, bool IsPayment, DateTime d)
        {
            int ActivityID;
            if (Data.DB.Activities.Count() == 0)
                ActivityID = 1;
            else
                ActivityID = Data.DB.Activities.Max(A => A.ActivityID) + 1;

            Activity newActivity = new Activity();
            newActivity.ActivityID = ActivityID;
            newActivity.CustID = custID;
            newActivity.Date = d;
            if (salesID.HasValue)
                newActivity.SalesID = salesID.Value;
            if (IsPayment)
            {
                newActivity.Payment = money;
                newActivity.Purchase = null;
            }
            else
            {
                newActivity.Purchase = money;
                newActivity.Payment = null;
            }

            Data.DB.Activities.InsertOnSubmit(newActivity);
            Data.DB.SubmitChanges();
        }

        public void Add(int custID, int salesID, decimal? payment, decimal purchase, DateTime d)
        {
            int ActivityID;
            if (Data.DB.Activities.Count() == 0)
                ActivityID = 1;
            else
                ActivityID = Data.DB.Activities.Max(A => A.ActivityID) + 1;

            Activity newActivity = new Activity();
            newActivity.ActivityID = ActivityID;
            newActivity.CustID = custID;
            newActivity.Date = d;
            newActivity.SalesID = salesID;
            newActivity.Payment = payment;
            newActivity.Purchase = purchase;

            Data.DB.Activities.InsertOnSubmit(newActivity);
            Data.DB.SubmitChanges();
        }

        public void Edit(int salesID, decimal totalWithTax, decimal paymentMade, string AccountName)
        {
            try
            {
                int accountID = Data.DB.Customers.First(S => S.CustomerName == AccountName).CustomerID;
                var activityes = from activities in Data.DB.Activities
                                 where activities.SalesID == salesID
                                 select activities;

                foreach (var v in activityes)
                {
                    v.Purchase = totalWithTax;
                    v.Payment = paymentMade;
                }

                Data.DB.SubmitChanges();
            }
            catch { }
        }
    }
}
